<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  Created by IntelliJ IDEA.
  User: 孤焰
  Date: 2021/6/9
  Time: 21:55
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>类别编辑</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/layuiadmin/style/admin.css" media="all">
</head>
<body>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-header">类别编辑</div>
        <div class="layui-card-body" style="padding: 15px;">
            <form class="layui-form" action="">
                <div class="layui-form-item">
                    <label class="layui-form-label">父类别</label>
                    <div class="layui-input-block">
                        <input type="text" lay-filter="pname" value="${category.parentId}" id="pname" name="pname" autocomplete="off" readonly="readonly" class="layui-input layui-disabled">
                    </div>
                </div>
                <input type="hidden" id="parentId" name="parentId">

                <div class="layui-form-item">
                    <label class="layui-form-label">类别名称</label>
                    <div class="layui-input-block">
                        <input type="text" id="name" value="${category.name}" name="name" lay-verify="required" autocomplete="off" placeholder="请输入类别名称"
                               class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">类别状态</label>
                    <div class="layui-input-block">
                        <c:if test="${category.status == 1}">
                            <input type="checkbox" id="status" name="status" lay-skin="switch" lay-text="启用|废弃" checked="">
                        </c:if>
                        <c:if test="${category.status == 2}">
                            <input type="checkbox" id="status" name="status" lay-skin="switch" lay-text="启用|废弃">
                        </c:if>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">排序等级</label>
                    <div class="layui-input-block">
                        <input type="text" id="sortOrder" value="${category.sortOrder}" name="sortOrder" lay-verify="sortOrder" autocomplete="off" placeholder="请输入排序等级"
                               class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item layui-layout-admin">
                    <div class="layui-input-block">
                        <div class="layui-footer" style="left: 0;">
                            <button class="layui-btn" lay-submit="" lay-filter="category-submit">立即提交</button>
                        </div>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>


<script src="${pageContext.request.contextPath}/layuiadmin/layui/layui.js"></script>
<script src="${pageContext.request.contextPath}/static/js/axios.min.js"></script>
<script>
    layui.config({
        base: '${pageContext.request.contextPath}/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'form'], function () {
        var layer = layui.layer;
        var form = layui.form;


        // 自定义表单验证
        form.verify({
            sortOrder: [/^[1-9]\d*$/, '排序编号必须为正整数']
        });

        /* 监听提交 */
        form.on('submit(category-submit)', function (data) {
            var category = data.field;
            delete category.pname;
            category.id = '${category.id}';

            // TODO category需要传递id值
            category.status = category.status == 'on' ? 1 : 2;

            axios.post('${pageContext.request.contextPath}/category/updateCategoryById', category).then(function (res) {
                // TODO 添加成功之后跳转至类别列表页
                if (res.data.code == 200) {
                    setTimeout(function () {
                        layer.msg("数据更新成功", {icon: 6});
                        setTimeout(function () {
                            parent.location.reload();// 刷新父窗口
                            window.parent.layer.closeAll();//关闭弹窗
                        }, 800);

                    }, 600);
                } else {
                    layer.msg(res.data.message, {icon: 5});
                }
            });
            return false;
        });
    });
</script>
</body>
</html>
